//
// Created by Administrator on 2020/7/17 0017.
//
#include "winsock.h"
#ifndef LIBRARY_DBUTIL_H
#define LIBRARY_DBUTIL_H
#include "mysql.h"
#include "User.h"
#include "Book.h"
#include "vector"
#include "BorrowRecord.h"
#include "TimeUtil.h"
#include <bits/stdc++.h>

class DBUtil
{
public:
    string szUsername;
    string szPwd;
    string szHost;
    string szDatabase;
    int nPort;
    MYSQL myCont;
    MYSQL_RES *result;
    MYSQL_ROW sql_row;
    MYSQL_FIELD *fd;
    bool isOpen;
    TimeUtil timeUtil;
public:
    User SelectUserById(int nUserId);
    DBUtil();
    ~DBUtil();
    bool OpenDB();
    bool CloseDB();
    bool AddUser(User user);
    User ReadUser(string strUserName,string strUserPWD);
    bool AddBook(Book book);
    bool SelectAllBook(vector<Book>&books);
    bool SelectAllUser(vector<User>&users);
    bool SelectBookByName(string strBookName,vector<Book>&books);
    bool SelectBookById(int nBookId,Book &book);
    bool DeleteBookById(int nBookId);
    bool AddBorrowRecord(BorrowRecord borrowRecord);
    bool ExtendBorrowRecord(int nRecordId);
    bool SelectBorrowRecordByRecordId(BorrowRecord &borrowRecord,int nRecordId);
    bool FinishBorrowRecord(int nRecordId,int nBookId);
    bool SelectAllBorrowRecord(vector<BorrowRecord>&borrowRecords);
    bool SelectBorrowRecordByUserId(vector<BorrowRecord>&borrowRecords,int nUserId,int nType);


};


#endif //LIBRARY_DBUTIL_H
